﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;

namespace SoIM
{
    /// <summary>
    /// 用户列表树
    /// </summary>
    /// <remarks>2013-10-30 by sutroon</remarks>
    public partial class ucUserTree : UserControl
    {
        public IList<Control> Nodes;
        private string faceFolder;
        public ucUserTree()
        {
            InitializeComponent();
            panelMain.AutoSize = true;
            panelMain.AutoSizeMode = AutoSizeMode.GrowOnly;
            this.Nodes = new List<Control>();
            faceFolder = UCL.ADF.ADFS.Instance.Site.GetFolder("Faces").ToString() + "\\";
        }

        public void uc_Init()
        {
            this.LoadData();
        }

        public void LoadData()
        {
            // 读取数据
            string xml = UCL.ADF.ADFS.Instance.Site.GetFileText("UserDB.soxf");
            if (xml.Length == 0) return;

            panelMain.Controls.Clear();

            ucGroupNode g = new ucGroupNode() { NodeText = "所有部门" };

            // 加载数据            
            string nodetag = "node", nametag = "Name", facetag = "Face";
            XElement doc = XElement.Parse(xml);
            int i = -1, level = 0;
            LoadData_Func(doc, g, nodetag, facetag, nametag, ref i, level);
            this.Nodes.Add(g);
            LoadNodes(g);

        }
        public void LoadData_Func(XElement xe, ucGroupNode g, string nodetag, string facetag, string nametag, ref int i, int level)
        {
            ucGroupNode g1;
            string dd;
            foreach (XElement xe1 in xe.Elements(nodetag))
            {
                i++;
                if (xe1.Attribute("IsGroup").Value == "0")
                {
                    dd = GetImageUrl(xe1.Attribute(facetag).GetValue());
                    g.Nodes.Add(new ucUserNode() { NodeText = xe1.Attribute(nametag).GetValue(), ImagePath = GetImageUrl(xe1.Attribute(facetag).GetValue()), NodeLevel = level, NodeIndex = i, Tag = xe1 });
                    continue;
                }
                g1 = new ucGroupNode() { NodeText = xe1.Attribute(nametag).GetValue(), NodeLevel = level, NodeIndex = i, ExpandAndCollapseHandler = ExpandAndCollapseEvent };
                if (xe1.HasElements) LoadData_Func(xe1, g1, nodetag, facetag, nametag, ref i, level + 1);
                g.Nodes.Add(g1);
            }
        }

        public void LoadNodes(ucGroupNode g)
        {
            if (g.Nodes.Count == 0) return;
            foreach (Control c in g.Nodes)
            {
                panelMain.Controls.Add(c);
                if (c is ucGroupNode) LoadNodes((ucGroupNode)c);
            }
        }

        private string GetImageUrl(string url)
        {
            if (url.Length == 0) return string.Empty;
            return faceFolder + System.IO.Path.GetFileName(url);
        }

        /// <summary>
        /// 展开和收起处理函数
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        /// <remarks>2013-10-31 by sutroon</remarks>
        public string ExpandAndCollapseEvent(ucGroupNode g)
        {
            panelMain.SuspendLayout();
            ExpandAndCollapseEvent_Func(g, g.SwitchButton.Text);
            panelMain.ResumeLayout();
            if (g.SwitchButton.Text == "+")
            {
                g.SwitchButton.Text = "-";
                g.NodeState = NodeStates.Expand;
            }
            else
            {
                g.SwitchButton.Text = "+";
                g.NodeState = NodeStates.Collapse;
            }
            return g.SwitchButton.Text;
        }
        private void ExpandAndCollapseEvent_Func(ucGroupNode g, string tag)
        {
            if (g.Nodes.Count == 0) return;
            ucGroupNode gTemp;
            foreach (Control c in g.Nodes)
            {
                if (c is ucGroupNode)
                {
                    gTemp = c as ucGroupNode;
                    gTemp.Visible = tag == "+";
                    if (gTemp.NodeState == NodeStates.Collapse) continue;
                    ExpandAndCollapseEvent_Func(gTemp, tag);
                }
                c.Visible = tag == "+";
            }
        }

        public ucUserNode FindUserNodeByIP(string ip)
        {
            if (string.IsNullOrEmpty(ip)) return null;
            XElement xe;
            ucUserNode u = null;
            foreach (Control c in panelMain.Controls)
            {
                xe = c.Tag as XElement;
                if (xe == null || xe.Attribute("LoginIP") == null || xe.Attribute("LoginIP").Value != ip) continue;
                u = c as ucUserNode;
                break;
            }
            return u;
        }
    }
}
